<?php
/**
 * Created by PhpStorm.
 * User: cqfut
 * Date: 2018/11/1
 * Time: 11:45
 */
require_once (ROOTDIR."common/db_utils.php");
class SumarryEvalModel {

    public function __construct() {

    }

    /***
     * 查询评价信息列表
     * @param null $title  关键字(用户名、姓名或学校)
     * @param int $beginIdx 从第几个信息开始返回
     * @param int $cnt 返回数量
     * @return array
     */
    public function getSummaryEvalList($title = null, $userId, $beginIdx = 0, $cnt = 20 ) {
        $titleStr = null;
        if ( !is_null($title) && strlen($title) > 0 ) {
            $titleStr="%$title%";
        }

        $sqlWhere = ""; // where 子句
        $args=array();

        // 添加关键字搜索
        if ( !is_null($titleStr)) {
            $sqlWhere = "  where `summary_title` like ? ";
            array_push($args, $titleStr);
        }

        $colNames = array();
        array_push($colNames, 'summary_id','summary_title', 'create_time', 'creater_id');

        $sql = DbUtil::genSelectSql($colNames, "summary_eval");
        $sql.=$sqlWhere;
        $sql.="  order by `summary_title` desc limit ".$beginIdx.", ".$cnt;
        // 特别注意：limit后面的数字不能作为$args
        //array_push($args, $beginIdx, $cnt);
        // 执行数据库查询语句
        $result = DbUtil::getInstance()->query2($sql, $args);
        $allRecords = array();
        if ( count($result) == 0 ) {
            return $allRecords;
        }

        $oneRecord=array();
        foreach ($result as $row ) {
            $oneRecord['id'] = intval($row['summary_id']);
            $oneRecord['title'] = urlencode($row['summary_title']);
            $oneRecord['creater_id'] = urlencode($row['creater_id']);
            $oneRecord['create_time'] = $row['create_time'];
            $oneRecord['allow_edit'] = $row['creater_id'] == $userId;
            array_push($allRecords, $oneRecord);
        }
        return $allRecords;
    }

    /***
     * 获取评价记录数量
     * @param null $title 标题
     * @return int
     */
    public function getSummaryEvalCnt($title = null) {

        $titleStr = null;
        if ( !is_null($title) && strlen($title) > 0 ) {
            $titleStr="%$title%";
        }

        $sqlWhere = ""; // where 子句
        $args=array();

        // 添加关键字搜索
        if ( !is_null($titleStr)) {
            $sqlWhere = "  where `summary_title` like ? ";
            array_push($args, $titleStr);
        }

        $sql= "select count(`summary_id`) from summary_eval ".$sqlWhere;
        $result = DbUtil::getInstance()->query2($sql, $args);
        if ( count($result) == 0 ) {
            return 0;
        }
        return intval($result[0][0]);
    }

    /**
     * 获取评价详情
     * @param $id
     * @param $userId
     * @return array|null
     */
    public function getSummaryEvalDetail($id, $userId) {

        $colNames = array('summary_id', 'summary_title', 'summary_content',
            'creater_id', 'create_time');
        $sql = DbUtil::genSelectSql($colNames, "summary_eval");
        $sql.= " where summary_id= ?";
        $args = array($id);

        $result = DbUtil::getInstance()->query2($sql, $args);
        if ( count($result) == 0 ) {
            return null;
        }
        $recordDetail = array();
        $row = $result[0];
        $recordDetail['id'] = intval($row['summary_id']);
        $recordDetail['title'] = urlencode($row['summary_title']);
        $recordDetail['content'] = urlencode($row['summary_content']);
        $recordDetail['creater_id'] = $row['creater_id'];
        $recordDetail['create_time'] = $row['create_time'];
        $recordDetail['allow_edit'] = $row['creater_id'] == $userId;
        return $recordDetail;
    }

    public function editSummaryEval($id, $title, $content, $createrId) {
        $colNames = array('summary_title', 'summary_content', 'create_time');
        $args = array($title, $content, date("Y-m-d h:i:s"));

        $sql = DbUtil::genUpdateSql($colNames, "summary_eval");
        $sqlWhere = "";
        $sqlWhere = DbUtil::genWhereSql($sqlWhere, 'summary_id');
        $sqlWhere = DbUtil::genWhereSql($sqlWhere, 'creater_id');
        array_push($args, $id, $createrId);
        $sql .= $sqlWhere;
        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }


    public function addSummaryEval($title, $content, $createrId) {
        $colNames = array('summary_title', 'summary_content', 'creater_id', 'create_time');
        $args = array($title, $content, $createrId, date("Y-m-d h:i:s"));

        $sql = DbUtil::genInsertSql($colNames, "summary_eval");

        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }

    public function isExist($id) {
        $id = strtolower($id);
        $sql= "select count(`user_id`) from users where `user_id` = ?";
        $result = DbUtil::getInstance()->query($sql, $id);
        if ( count($result) == 0 ) {
            return 0;
        }
        return intval($result[0][0]);
    }

    public function delSummaryEval($id, $createrId) {
        $colNames = array('summary_id', 'creater_id');
        $args = array($id, $createrId);

        $sql= DbUtil::genDelSql($colNames, "summary_eval");
        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }

    public function isAdmin($id) {
        $id = strtolower($id);
        $sql= "select count(*) from privilege where rightstr='administrator' and user_id = ?";
        $result = DbUtil::getInstance()->query($sql, $id);
        return intval($result[0][0]);
    }
}